package com.cloud.adrian.novel.author.manager.feign;

import com.cloud.adrian.novel.author.manager.chache.AuthorInfoCacheManager;
import com.cloud.adrian.novel.author.novel.dto.AuthorInfoDto;
import com.cloud.adrian.novel.book.dto.req.*;
import com.cloud.adrian.novel.book.dto.resp.BookChapterContentRespDto;
import com.cloud.adrian.novel.book.dto.resp.BookChapterRespDto;
import com.cloud.adrian.novel.book.dto.resp.BookInfoRespDto;
import com.cloud.adrian.novel.book.feign.BookFeign;
import com.cloud.adrian.novel.common.auth.UserHolder;
import com.cloud.adrian.novel.common.req.PageReqDto;
import com.cloud.adrian.novel.common.resp.PageRespDto;
import com.cloud.adrian.novel.common.resp.RestResp;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Component;

/**
 * 小说微服务调用feign客户端
 * @Author: xiangguiliang
 * @version: 1.0
 * @since: 2024/05/30
 */
@Component
@AllArgsConstructor
public class BookFeignManager {

    private final BookFeign bookFeign;

    private final AuthorInfoCacheManager authorInfoCacheManager;

    public RestResp<Void> publishBook(BookAddReqDto dto) {
        AuthorInfoDto author = authorInfoCacheManager.getAuthor(UserHolder.getUserId());
        dto.setAuthorId(author.getId());
        dto.setPenName(author.getPenName());
        return bookFeign.publishBook(dto);
    }

    public RestResp<PageRespDto<BookInfoRespDto>> listPublishBooks(BookPageReqDto dto) {
        dto.setAuthorId(UserHolder.getAuthorId());
        return bookFeign.listPublishBooks(dto);
    }

    public RestResp<Void> publishBookChapters(Long bookId, ChapterAddReqDto dto) {
        dto.setBookId(bookId);
        dto.setAuthorId(UserHolder.getAuthorId());
        return bookFeign.publishBookChapters(dto);
    }

    public RestResp<PageRespDto<BookChapterRespDto>> listBookChapters(Long bookId, PageReqDto dto) {
        ChapterPageReqDto reqDto = new ChapterPageReqDto();
        reqDto.setBookId(bookId);
        reqDto.setPageNum(dto.getPageNum());
        reqDto.setPageSize(dto.getPageSize());
        return bookFeign.listBookChapters(reqDto);
    }

    public RestResp<BookChapterContentRespDto> getChapterContent(Long id) {
        ChapterContentReqDto dto = new ChapterContentReqDto();
        dto.setChapterId(id);
        dto.setAuthorId(UserHolder.getAuthorId());
        return bookFeign.getChapterContent(dto);
    }

    public RestResp<Void> updateChapter(Long id, ChapterAddReqDto dto) {
        dto.setId(id);
        return bookFeign.updateChapter(dto);
    }

    public RestResp<Void> deleteChapter(Long id) {
        ChapterContentReqDto dto = new ChapterContentReqDto();
        dto.setAuthorId(UserHolder.getAuthorId());
        dto.setChapterId(id);
        return bookFeign.deleteChapter(dto);
    }
}
